package org.example.web.controller;

import org.example.biz.DepartmentBiz;
import org.example.biz.EmployeeBiz;
import org.example.oa.entity.Employee;
import org.example.oa.global.Contant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * 对员工增删改查的Controller
 */
@Controller("employeeController")
@RequestMapping("/employee")//设置本控制器类的访问路径
public class EmployeeController {

    @Autowired
    private DepartmentBiz departmentBiz;
    @Autowired
    private EmployeeBiz employeeBiz;

    //获取数据库员工列表
    @RequestMapping("/list")
    public String list(Map<String,Object> map){
        map.put("list", employeeBiz.getAll());
        //根据spring-web。xml视图解析器解析为/WEN-INF/pages/department_list.jsp
        return "employeeBiz_list";
    }


    //打开添加员工的页面
    @RequestMapping("/to_add")
    public String toAdd(Map<String,Object> map){
        map.put("employee", new Employee());
        //添加员工时需要得到所有部门信息和职位信息
        map.put("dlist", departmentBiz.getAll());
        map.put("plist", Contant.getPosts ());
        return "employee_add";
    }
    //添加员工，重定向到list请求
    @RequestMapping("/add")
    public String add(Employee employee){
        employeeBiz.add(employee);
        return "redirect:list";
    }


    //跳转到更新员工信息页面
    @RequestMapping(value = "/to_update", params = "sn")
    public String toUpdate(String sn, Map<String, Object> map){
        map.put("employee", employeeBiz.get(sn));
        //更新员工时需要得到所有部门信息和职位信息
        map.put("dlist", departmentBiz.getAll());
        map.put("plist", Contant.getPosts ());
        return "employee_update";
    }
    //更新员工信息,重定向到list请求
    @RequestMapping("/update")
    public String update(Employee employee){
        employeeBiz.edit(employee);
        return "redirect:list";
    }


    //删除员工,重定向到list请求
    @RequestMapping(value = "/remove", params = "sn")
    public String remove(String sn){
        employeeBiz.remove(sn);
        return "redirect:list";
    }
}
